package com.mtbu.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import my.myorm.tool.jdbc.ResultList;
import my.myorm.tool.jdbc.SQLExecutor;
import my.myorm.tool.jdbc.SQLManager;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import com.mtbu.bean.BppCata;
import com.mtbu.comm.Util;
import com.util.SuperString;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Xu Jianbo
 * @version 1.0
 *          Date: 2010-12-8
 *          Time: 16:00:25
 */
public class DataUtil {

    private static final Log logger = LogFactory.getLog(DataUtil.class);

    
    private static List<BppCata> listBppCata;
    private static Map<Integer, BppCata> mapBppCata;
    
    private static List<Integer> listStatus;
    private static Map<Integer, String> mapStatus;  
    
    private static List<Integer> listVT;
    private static Map<Integer, String> mapVT;  
    
    private static List<Integer> listVideoLanguage;
    private static Map<Integer, String> mapVideoLanguage; 
    
    private static List<Integer> listVideoType;
    private static Map<Integer, String> mapVideoType;  
    
    public static void reloadBppCata() {
        listBppCata = new ArrayList<BppCata>();
        mapBppCata = new HashMap<Integer, BppCata>();
        SQLExecutor dbconn = null;
        try {
            dbconn = SQLManager.getSQLExecutor("EPGDS");
            String sql = "select * from bpp_cata order by rank desc,id";
            ResultList rs = dbconn.query(sql, false, null);
            for(int i = 0; i < rs.size(); i++) {
                BppCata catalog = rs.get(i).get(BppCata.class);
                if(catalog.getPID()==0){
                	listBppCata.add(catalog);
                	loadListBppCata(catalog, rs);
                    mapBppCata.put(catalog.getId(),catalog);
                }
            }
            logger.info("*************** Reload BppCata ...... OK! ****************");
        } catch(Exception e) {
            e.printStackTrace();
            logger.info("*************** Reload BppCata ...... FAIL! ****************");
        }
    }
    public static void loadListBppCata(BppCata catalog,ResultList rs){
    	try {
	    	for(int i=0;i<rs.size();i++){
	    		BppCata tmp = rs.get(i).get(BppCata.class);
	    		if(catalog.getId()==tmp.getPID()){
	    			if(catalog.getSublist()==null) catalog.setSublist(new ArrayList<BppCata>());
	    			catalog.getSublist().add(tmp);
	    			loadListBppCata(tmp, rs);
	    		}
	    	}
	    	mapBppCata.put(catalog.getId(), catalog);
    	}catch(Exception e) {
            e.printStackTrace();
            logger.info("*************** Reload BppCata ...... FAIL! ****************");
        }
    }
    public static List<BppCata> getListBppCata() {
        if(listBppCata == null) {
            reloadBppCata();
        }
        return listBppCata;
    }

    public static Map<Integer, BppCata> getMapBppCata() {
        if(mapBppCata == null) {
            reloadBppCata();
        }
        return mapBppCata;
    }

    public static BppCata getBppCata(int id, boolean bNotNull) {
        BppCata catalog = getMapBppCata().get(id);
        return catalog == null ? (bNotNull ? new BppCata() : null) : catalog;
    }
    public static String getBppCataTitleWithParent(int id,boolean isWithParent) {
    	BppCata catalog = getMapBppCata().containsKey(id)?getMapBppCata().get(id):null;
        BppCata parent = catalog==null?null:(getMapBppCata().containsKey(catalog.getPID())?getMapBppCata().get(catalog.getPID()):null);
        return (isWithParent?(parent==null?"":parent.getTitle()+" > "):"")+(catalog==null?"":catalog.getTitle());
    }
    public static String getBppCataTitleWithParent(int id) {
        return getBppCataTitleWithParent(id,true);
    }    
   	public static void reloadVideoType() {
        mapVideoType = new HashMap<Integer, String>();
        listVideoType = new ArrayList<Integer>();
        String stringVideoType = Util.getProperties("CHANNEL_VIDEO_TYPE");
        String[] arrayVideoType = stringVideoType.split(",");
        for(int i=0;i<arrayVideoType.length;i++){
        	String[] detailVideoType = arrayVideoType[i].split(":");
        	mapVideoType.put(SuperString.getInt(detailVideoType[0]),detailVideoType.length<2?"":detailVideoType[1]);
        	listVideoType.add(SuperString.getInt(detailVideoType[0]));
        }
    }
	public static Map<Integer, String> getMapVideoType() {
		if(mapVideoType== null) {
            reloadVideoType();
        }
		return mapVideoType;
	}
	public static String getVideoType(Integer id) {
		if(mapVideoType== null) {
            reloadVideoType();
        }
		return mapVideoType.get(id)==null?"":mapVideoType.get(id);
	}
	
	
   public static List<Integer> getListVideoType() {
	   if(listVideoType== null) {
	        reloadVideoType();
	    }
		return listVideoType;
   }
   
   public static void reloadVT() {
       mapVT = new HashMap<Integer, String>();
       listVT = new ArrayList<Integer>();
       String stringVT = Util.getProperties("CHANNEL_VT");
       String[] arrayVT = stringVT.split(",");
       for(int i=0;i<arrayVT.length;i++){
       	String[] detailVT = arrayVT[i].split(":");
       	mapVT.put(SuperString.getInt(detailVT[0]),detailVT.length<2?"":detailVT[1]);
       	listVT.add(SuperString.getInt(detailVT[0]));
       }
   }
	public static Map<Integer, String> getMapVT() {
		if(mapVT== null) {
           reloadVT();
       }
		return mapVT;
	}
	public static String getVT(Integer id) {
		if(mapVT== null) {
           reloadVT();
        }
		String resString = mapVT.get(id)==null? ""+id  : mapVT.get(id);
		
		return resString;
	}
	
	
  public static List<Integer> getListVT() {
	   if(listVT== null) {
	        reloadVT();
	    }
		return listVT;
  
  }
  
  public static void reloadVideoLanguage() {
      mapVideoLanguage = new HashMap<Integer, String>();
      listVideoLanguage = new ArrayList<Integer>();
      String stringVideoLanguage = Util.getProperties("CHANNEL_VIDEO_LANGUAGE");
      String[] arrayVideoLanguage = stringVideoLanguage.split(",");
      for(int i=0;i<arrayVideoLanguage.length;i++){
      	String[] detailVideoLanguage = arrayVideoLanguage[i].split(":");
      	mapVideoLanguage.put(SuperString.getInt(detailVideoLanguage[0]),detailVideoLanguage.length<2?"":detailVideoLanguage[1]);
      	listVideoLanguage.add(SuperString.getInt(detailVideoLanguage[0]));
      }
  }
	public static Map<Integer, String> getMapVideoLanguage() {
		if(mapVideoLanguage== null) {
          reloadVideoLanguage();
      }
		return mapVideoLanguage;
	}
	public static String getVideoLanguage(Integer id) {
		if(mapVideoLanguage== null) {
          reloadVideoLanguage();
      }
		return mapVideoLanguage.get(id)==null?"":mapVideoLanguage.get(id);
	}
	
	
 public static List<Integer> getListVideoLanguage() {
	   if(listVideoLanguage== null) {
	        reloadVideoLanguage();
	    }
		return listVideoLanguage;
 }
 

	
	public static void reloadStatus() {
     mapStatus = new HashMap<Integer, String>();
     listStatus = new ArrayList<Integer>();
     String stringStatus = Util.getProperties("CHANNEL_STATUS");
     String[] arrayStatus = stringStatus.split(",");
     for(int i=0;i<arrayStatus.length;i++){
     	String[] detailStatus = arrayStatus[i].split(":");
     	mapStatus.put(SuperString.getInt(detailStatus[0]),detailStatus.length<2?"":detailStatus[1]);
     	listStatus.add(SuperString.getInt(detailStatus[0]));
     }
 }
	public static Map<Integer, String> getMapStatus() {
		if(mapStatus== null) {
         reloadStatus();
     }
		return mapStatus;
	}
	public static String getStatus(Integer id) {
		if(mapStatus== null) {
         reloadStatus();
     }
		return mapStatus.get(id)==null?"":mapStatus.get(id);
	}
	
	
public static List<Integer> getListStatus() {
	   if(listStatus== null) {
	        reloadStatus();
	    }
		return listStatus;
}

    /**
     * 数据初始化操作
     */
    public static void init() {
        
    }

}
